<!--  -->
<template>
  <div>
    <el-tree
      :data="menus"
      :props="defaultProps"
      :expand-on-click-node="false"
      @node-click="handleNodeClick"
      node-key="catId"
      show-checkbox
      :default-expanded-keys="expandedKey"
      draggable
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            v-if="node.level <=2"
            type="text"
            size="mini"
            @click="() => append(data)"
          >Append</el-button>
          <el-button
            v-if="node.childNodes.length == 0"
            type="text"
            size="mini"
            @click="() => remove(node, data)"
          >Delete</el-button>
        </span>
      </span>
    </el-tree>

    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-form ref="form" :model="category" label-width="80px">
           <el-form-item label="菜单名字">
               <el-input v-model="category.name"></el-input>
          </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCategory">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    return {
      category:{name:"",parentCid:0,catLevel:0,showStatus:1,sort:0,productCount:0},
      dialogVisible:false,
      menus: [],
      expandedKey: [2],
      defaultProps: {
        children: "childMean",
        label: "name",
      },
    };
  },
  methods: {
    handleClose(){

    },
    addCategory(){
        this.$http({
        url: this.$http.adornUrl('/product/category/save'),
        method: 'post',
        data: this.$http.adornData(this.category, false)
        }).then(({ data }) => { 
            this.dialogVisible =false;
             this.$message({
            type: "success",
             message: "添加菜单成功!",
            });
            this.expandedKey=[this.category.parentCid];
            this.getMenus();
        });
    },
    handleNodeClick(data) {
      console.log(data);
    },
    getMenus() {
      this.$http({
        url: this.$http.adornUrl("/product/category/list/tree"),
        method: "get",
        //   params: this.$http.adornParams({
        //     'page': this.pageIndex,
        //     'limit': this.pageSize,
        //     'roleName': this.dataForm.roleName
        //   })
      }).then(({ data }) => {
        console.log("成功获取到菜单数据" + data.data);
        this.menus = data.data;
      });
      
    },
    append(data) {
      this.category.parentCid =data.catId;
      this.category.catLevel =data.catLevel*1+1;
      this.dialogVisible =true;
      console.log("点击了添加按钮" +data);
    },

    remove(node, data) {
      var ids = [data.catId];
      //加入单引号 用$符号引用
      this.$confirm(`是否删除【${data.name}】此菜单`, "消息确认提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/product/category/delete"),
          method: "post",
          data: this.$http.adornData(ids, false),
        }).then(({ data }) => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getMenus();
          this.expandedKey = [node.parent.data.catId];
        });
      });
    },
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合

  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getMenus();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped>
</style>